package com.hhc.repository;

import com.hhc.entity.StudentCourseSelection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *   学生选课记录数据库访问层
 * </p>
 * @author pc
 */
@Repository
public interface StudentCourseSelectionRepository extends JpaRepository<StudentCourseSelection, Long> {

    boolean existsByCourseId(Long courseId);

    Optional<StudentCourseSelection> findByStudentIdAndCourseId(Long studentId, Long courseId);

    @Modifying
    @Query("UPDATE StudentCourseSelection s SET s.selectionStatus = 0 WHERE s.studentId = ?1 AND s.courseId = ?2")
    void updateSelectionStatusByStudentIdAndCourseId(Long studentId, Long courseId);

    boolean existsByStudentIdAndCourseId(Long studentId, Long courseId);

    // 添加这个方法来支持 findByStudentId 查询
    List<StudentCourseSelection> findByStudentId(Long studentId);
}
